/**
  ******************************************************************************
  * @file    nuc it.c
  * @brief   Interrupt Service Routines.
  ******************************************************************************
**/
#include "gd32f10x.h"
#include "osObjects.h"
#include "sys.h"
#include "pulse.h"
#include "dataproc.h"
#include "daemonprocess.h"
#include "systask.h"
#include "stdio.h"

#define  ARM_MATH_CM3
#include <math.h>
#include "arm_math.h"

volatile  uint8_t bpvSub,bpvAdd; 
extern uint32_t ucTick;
uint32_t mes_buff[4];
timer_pulse_type mj_pulse;
void CtrSimulink(void);
void JiaMiHandle(uint32_t rx0, uint32_t rx1, uint32_t rx2, uint32_t rx3);
void q15_data_print(const char *Name, int32_t q15_data);
char pt_buff[64];

volatile q31_t ma,mb,mc;

struct
{
    float f_disp;
    int32_t i_disp;
    float sub_val;
} disp_obj;

typedef union
{
    int16_t w[2];
    uint16_t uw[2];
    int32_t ival;
} q_data_type;

volatile q_data_type q_cal_add;
volatile q_data_type q_cal_sub;
volatile q_data_type q_cal_ad2;
volatile q_data_type q_cal_ad1;

uint32_t in_list[] =
{
    300UL,600UL,
    6UL<<16,8UL<<16,
};

/*****************************************************************************//*!
*
*****************************************************************************/
void mjtask(void   *argument)
{
    uint64_t u64t;
    uint32_t u32a,u32b;
    
    rt_err_t err_st;

    pulse_init(&mj_pulse, ucTick);

    daemon_reg(600,0);
    
    for(;;)
    {
        in_list[0]++;
        if(in_list[0] & 0x01)
        {
            in_list[2]++;
            in_list[3]++;
        }
        else
        {
            in_list[2]--;
            in_list[3]--;
        }
        CtrSimulink();
        PulseCreat(&mj_pulse, ucTick);
        daemon_take();
        disp_obj.i_disp = SubAdd_x(disp_obj.f_disp, disp_obj.i_disp, disp_obj.sub_val);


        q_cal_add.ival = q_cal_ad1.ival + q_cal_ad2.ival;
        q_cal_sub.ival = q_cal_ad1.ival - q_cal_ad2.ival;
        
        err_st = rt_mq_recv(qid_mj,mes_buff, Q_MJ_SIZE, ms_ticks(50));
        if(RT_EOK == err_st)
        {

        }
        else
        {

        }

        if(mj_pulse.bPulse_100ms)
        {
            u32a = in_list[0];
            u32b = in_list[1];
        }
        else
        {
            u32a = in_list[2];
            u32b = in_list[3];
        }

        u64t = (uint64_t)u32a * u32b;

        
        
        multAcc_32x32_keep32_R(ma,mb,mc);
    }
}

void q15_data_print(const char *Name, int32_t q15_data)
{
    uint32_t Cnt;
    Cnt = sprintf(pt_buff,"%s: %f\n",Name,((float)q15_data)/32768);
    pt_buff[Cnt] = 0;
    rt_hw_console_output(pt_buff);
}

void CtrSimulink(void)
{
    if(!mj_pulse.bPulse_250ms)
        return;

    if(SysRunSt.o3_box.b_run_st)
    {
        if(SysIoSt.o3_temp_disp > SysRunSt.o3_box.TempSet*10)
            SysIoSt.o3_temp_disp--;
        if(SysIoSt.o3_temp_disp < SysRunSt.o3_box.TempSet*10)
            SysIoSt.o3_temp_disp++;

        if(SysIoSt.o3_humi_disp > SysRunSt.o3_box.HumiSet*10)
            SysIoSt.o3_humi_disp--;
        if(SysIoSt.o3_humi_disp < SysRunSt.o3_box.HumiSet*10)
            SysIoSt.o3_humi_disp++;

        if(SysIoSt.o3_o_disp > SysRunSt.o3_box.o3Set)
            SysIoSt.o3_o_disp--;
        if(SysIoSt.o3_o_disp < SysRunSt.o3_box.o3Set)
            SysIoSt.o3_o_disp++;
    }
    else
    {
        if(SysIoSt.o3_temp_disp > 1000)
            SysIoSt.o3_temp_disp--;
        if(SysIoSt.o3_temp_disp < 1000)
            SysIoSt.o3_temp_disp++;

        if(SysIoSt.o3_humi_disp > 5000)
            SysIoSt.o3_humi_disp--;
        if(SysIoSt.o3_humi_disp < 5000)
            SysIoSt.o3_humi_disp++;

        if(SysIoSt.o3_o_disp > 0)
            SysIoSt.o3_o_disp--;
        if(SysIoSt.o3_o_disp < 0)
            SysIoSt.o3_o_disp++;
    }

    if(SysRunSt.rf_box.b_run_st)
    {
        if(SysIoSt.rf_temp_disp > SysRunSt.rf_box.TempSet*10)
            SysIoSt.rf_temp_disp--;
        if(SysIoSt.rf_temp_disp < SysRunSt.rf_box.TempSet*10)
            SysIoSt.rf_temp_disp++;

        if(SysIoSt.rf_humi_disp > SysRunSt.rf_box.HumiSet*10)
            SysIoSt.rf_humi_disp--;
        if(SysIoSt.rf_humi_disp < SysRunSt.rf_box.HumiSet*10)
            SysIoSt.rf_humi_disp++;


    }
    else
    {
        if(SysIoSt.rf_temp_disp > 1000)
            SysIoSt.rf_temp_disp--;
        if(SysIoSt.rf_temp_disp < 1000)
            SysIoSt.rf_temp_disp++;

        if(SysIoSt.rf_humi_disp > 5000)
            SysIoSt.rf_humi_disp--;
        if(SysIoSt.rf_humi_disp < 5000)
            SysIoSt.rf_humi_disp++;
    }
}


